package com.sino.operation.util;

import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * TOD
 *
 * @author Administrator
 * date 2021/9/14 0014 22:15
 */
public class CopyUtil {


    public static <T> T copy(Object source,Class<T> clazz){
        if(source == null){
            return null;
        }
        T obj = null;
        try{
            obj = clazz.newInstance();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        BeanUtils.copyProperties(source , obj);
        return obj;
    }

    /**
     * @Description //TODO 复制对象封装方法
     * @Param [source clazz]来源对象集合 目标对象类
     * @return 目标对象
     *@Author Administrator
     * @Date 0:26 2021/3/29
     **/
    public static <T> List<T> copyList(List source , Class<T> clazz){
        ArrayList<T> target = new ArrayList<>();
        if(!CollectionUtils.isEmpty(source)){
            for(Object c : source){
                T obj = copy(c,clazz);
                target.add(obj);
            }
        }
        return target;
    }

}